﻿// Created by Ken   
// this class is the main class to test all the other class
// Be sure to include package board
//********************************************************
// Modified by Ayaz Maroof
// Date: 7 March 2012
// Changes: Changed the parameters for the eW.showQuestion and eW.showMC function call
//*********************************************************
// Modified by Ming
// Date: 11 March 2012
// Changes: Changed the parameters for diceW.rollingSinglePlayer function call
//*********************************************************
// Modified by Syrings
// Date: 12 March 2012 - 15 March 2012
// Changes: The structure of the function, put the different activity method into the appropriate cell class
//*********************************************************
// Modified by Tung
// Date: 13 April 2012
// Changes: Create the InputCheck method to check the input is "Y/N" to decide terminate the game or not

import board.*;
import player.*;
import dice.*;
import cell.Cell;

import java.util.Scanner;

public class Main {
	// static variable
	private static Board gameBoard ;
	private static int numberOfPlayer;
	private static Player[] playerArr;
	private static Cell[] cellBoard;
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// Set Up
		PlayerIO io = new PlayerIO();
		//io.getInput(playerArr, gameBoard);
		
		//numberOfPlayer = io.getNumOfPlayer();
		numberOfPlayer = io.inputNumOfPlayer();
		playerArr = new Player[numberOfPlayer];
		gameBoard=new Board();
		// Call control class  set up method to set up 
		PlayerDataProcessor processor = new PlayerDataProcessor();
		processor.setUp(playerArr, numberOfPlayer, gameBoard);
		
		Move moveP = new Move();
		moveP.updateBoard(playerArr);
		gameBoard= moveP.getTemBoard();
		
		PrintTable pT = new PrintTable();
		pT.repaint(gameBoard);
		
		boolean flag = true;
		boolean flagYN = false;
		// Setup Done
		String ans;
		do {
			// Now it is rolling the dice
			for(int l=0; l<numberOfPlayer; l++) 
			{
				RollDice rollDice = new RollDice();
				// Roll the first player				
				if(playerArr[l].getCell() != 24 || playerArr[l].getSEPoint()>=0){
					gameBoard.getCellArr()[playerArr[l].getCell()]--;
					int result = rollDice.roll(playerArr[l].getPlayerID());
					playerArr[l].setCell((playerArr[l].getCell() + result)%28);
				    
				    // Update user GridX and GridY
				    PlayerDataProcessor pDP = new  PlayerDataProcessor();
				    pDP.updatePlayerGrid(playerArr[l], gameBoard);
				    
				    // Update the player array by replace the new version Player
				    //temPArr[index] = temP;
				    
				    // Update Board layout
				    Move movePlayer = new Move();
				    movePlayer.updateBoard(playerArr);
				    gameBoard= movePlayer.getTemBoard();
				    
				    io.printSinglePlayerInfo(playerArr[l]); // print out temP info
				    
				    // Print out the new Board
				    PrintTable printT = new PrintTable();
				    printT.repaint(gameBoard);
				}
				// If hit on a cell which has activity, something happen here
				cellBoard = gameBoard.setCellBoard();
				cellBoard[playerArr[l].getCell()].TriggerCell(playerArr[l]);
				 if(playerArr[l].getSEPoint() > 30){
					 System.out.println("Your SE point is more than 30 now! You win the game!");
					 return;
				 }
			}
			flagYN=InputCheck(flag, flagYN);	
		}while(flagYN == true);
	}
	
	public static boolean InputCheck(boolean flag, boolean flagYN)
	{
		String ans=" ";
		do
		{
			System.out.println("Do you want to continue for the next round? (y/n) ");
			Scanner s = new Scanner(System.in);
			ans = s.next();	
			if (ans.equals("y") || ans.equals("Y"))
			{
				flag = false;
				flagYN = true;
			}
			else if (ans.equals("n") || ans.equals("N"))
			{
				System.out.println("GAME OVER!");
				flag = false;
				flagYN = false;
			}
		} while (flag == true);
		return flagYN;
	}
}